<?php

/**
 * +---------------------------------------------------------------
 * | Author: 北京永青宇创科技有限公司
 * | Date: 2020/03/12
 * | 版本: 6.0
 * +---------------------------------------------------------------
 * | CopyRight: 北京永青宇创科技有限公司 All rights reserved.
 * +---------------------------------------------------------------
 * | 版权声明：云豹不是一个自由软件，是云豹官方推出的商业源码。
 * | 严禁在未经许可的情况下拷贝、复制、传播、使用云豹的任意代码
 * | 如有违反，请立即删除，否则您将面临承担相应，法律责任的风险。
 * | 如果需要取得官方授权，请联系官方 https://www.gepardshop.com
 * +---------------------------------------------------------------
 */

namespace app\ticket\controller;

use app\ticket\model\TicketProduct as TPModel;
use app\ticket\model\TicketCategory as TCModel;
use app\ticket\model\TicketSolution as TSModel;
use app\ticket\model\Ticket as TModel;
use think\facade\Validate;

class Index extends TicketHome {
	public function index()
	{

	    //取得四个问题类型
        $category_list = TCModel::all(function($query){
           $query->order('is_recommend desc,id asc')->limit(3)->field('id,title,status,icon,product_id,description');
        });

        //拿到所有产品列表
        $product_list = TPModel::all(function($query){
            $query->order('sort','asc');
        });

        $this->assign('category_list', $category_list);
        $this->assign('product_list', $product_list);

		return $this->fetch();
	}

	public function categoryList()
	{
		$this->set_step(2);

		$product_id = input('id');
		
		if(empty($product_id)){
		    $this->error("无效的产品");
        }
		$category_list = TCModel::all();


		//热点解决方案   => product_id一致，按点赞数排名
        $hot_solution = TSModel::all(function($query) use ($product_id) {
           $query->where('product_id',$product_id)->order('num_support','desc');
        });

		$this->assign('category_list',$category_list);
		$this->assign('hot_solution_list',$hot_solution);
        $this->assign('product_id',$product_id);
		return $this->fetch();
	}

	public function solutionList()
	{
		$this->set_step(3);
		$category_id = input("category_id");
		$product_id = input("product_id");
		if(empty($category_id)){
		    $this->error('无效的问题分类');
        }

		$solution_list = TSModel::all(function($query) use($category_id,$product_id){
		   $query->where('category_id',$category_id);
			$query->where('product_id',$product_id);
        });

        //获取解决率最高的 8 条热门知识点   =>所有解决方案，按点赞数排名
        $hot8_solution_list = TSModel::all(function($query) use($product_id){
	        $query->where('product_id',$product_id);
           $query->order('num_support','desc')->limit(8);
        });

		$this->assign('solution_list',$solution_list);
		$this->assign('hot8_solution_list',$hot8_solution_list);
        $this->assign('product_id',$product_id);
        $this->assign('category_id',$category_id);
		return $this->fetch();
	}

	public function create()
	{
		$this->set_step(4);

		$product_id = input("product_id");
		$category_id = input("category_id");

        $this->assign('product_id',$product_id);
        $this->assign('category_id',$category_id);
		return $this->fetch();
	}



	public function add()
	{
        if(request()->isPost()){
            $form = input("");
            $user_id = $this->auth->id;
            if(empty($user_id)){
                $this->error("非法请求");
            }
            $form['user_id'] = $user_id;
            $validate = new \app\ticket\validate\Ticket();
            if(! $validate->scene('add')->check($form)){
                $this->error($validate->getError());
            }

            //存入数据库
            $ticket = new TModel();
            if($ticket->allowField(true)->save($form)){
                $this->success("提交工单成功",'ticket/user/index');
            }else{
                $this->error("提交工单失败");
            }


        }else {
            $this->error('工单已收到');
        }
	}
}